// views/mine/mynumber/index.js
var BASE_URL = getApp().globalData.BASE_URL

Page({

  /**
   * 页面的初始数据
   */
  data: {
    phone: '',
    btnshow: true,
    popshow: false,
    email:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getcode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var phone = wx.getStorageSync("phoneNumber")
    var USER= wx.getStorageSync("USER")

    if (phone) {
      this.setData({
        phone: phone
      })
    } else {
      this.setData({
        phone: '空'
      })
    }
    
    if (USER.email) {
      this.setData({
        email: USER.email
      })
    } else {
      this.setData({
        email: '空'
      })
    }
  },

  startmessage: function (e) {
    // console.log(e)

  },
  completemessage: function (e) {
    // console.log(e)
    this.setData({
      popshow: true,
      btnshow: false
    })
  },

  iknow: function () {
    this.setData({
      popshow: false
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  //获取手机号
  getPhoneNumber: function (e) {
    var _this = this;
    wx.checkSession({
      success: function () {
        wx.request({
          url: `${BASE_URL}/ajax/getUserPhone`, //自己的解密地址
          data: {
            encryptedData: e.detail.encryptedData,
            iv: e.detail.iv,
            sessionKey: wx.getStorageSync("sessionkey")
          },
          method: "post",
          header: {
            'content-type': 'application/json'
          },
          success: function (res) {
            console.log(res, e)
            if (e.detail.errMsg == "getPhoneNumber:ok") {
              wx.setStorage({
                key: "phoneNumber",
                data: res.data.phoneNumber
              })
              _this.setData({
                phone: res.data.phoneNumber
              })
              var opid = wx.getStorageSync('openid')
              var unid = wx.getStorageSync('unionid')
              var data = wx.getStorageSync('data')
              console.log('没授权过,获取手机号',
                _this.data.pid,
                opid,
                res.data.phoneNumber,
                'xiaochengxv'
              )
              console.log(opid, unid, res.data.phoneNumber)
              wx.request({
                url: `${BASE_URL}/ajax/wechatinfo`, //仅为示例，并非真实的接口地址
                data: {
                  avatarUrl: data.avatarUrl,
                  city: data.avatarUrl,
                  country: data.country,
                  gender: data.gender,
                  language: data.language,
                  nickName: data.nickName,
                  openId: opid,
                  province: data.avatarUrl,
                  unionId: unid,
                  phonenumber: res.data.phoneNumber
                },
                method: "POST",
                header: {
                  'content-type': 'application/json' // 默认值
                },
                success(res) {
                  console.log(res, '授权手机号找到diy用户')
                  wx.setStorage({
                    key: "USER",
                    data: res.data
                  })
                }
              })
            }
          }
        })
      }
    })
  },
  getcode: function () {
    wx.login({
      success: res => {
        wx.request({
          url: `${BASE_URL}/ajax/login`,
          data: {
            code: res.code
          },
          method: "GET",
          success: res => {
            // 获取到用户的 openid
            wx.setStorage({
              key: "openid",
              data: res.data.openid
            })
            wx.setStorage({
              key: "sessionkey",
              data: res.data.session_key
            })
            wx.setStorage({
              key: "unionid",
              data: res.data.unionid
            })
          }
        });
      }
    });
  },
})